/*******************************************************************************
 * Copyright (c) 2014 Steffen Mikkelsen
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Steffen Mikkelsen - initial API and implementation
 ******************************************************************************/
package com.subsecret.secureconnection.securestream.layers.obfuscation;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.subsecret.secureconnection.securestream.SecureStreamLayer;
import com.subsecret.secureconnection.securestream.layers.helper.DiffieHellmanGenerator;
import com.subsecret.secureconnection.securestream.stream.obfuscation.ObfuscationInputStream;
import com.subsecret.secureconnection.securestream.stream.obfuscation.ObfuscationOutputStream;
import com.subsecret.tuples.Tuple;
import com.subsecret.tuples.Tuple.Tuple2;

public class ObfuscationLayer implements SecureStreamLayer {
    
    @Override
    public int getLayerID() {
        return 30;
    }
    
    @Override
    public String getLayerDescription() {
        return "Obfuscation Layer";
    }
    
    @Override
    public Tuple2<InputStream, OutputStream> createLayerClient(InputStream in, OutputStream out) throws IOException {
        byte[] secret = DiffieHellmanGenerator.generateKeyClient(in, out);
        in = new ObfuscationInputStream(in, secret);
        out = new ObfuscationOutputStream(out, secret);
        out.flush();
        return Tuple.of(in, out);        
    }
    
    @Override
    public Tuple2<InputStream, OutputStream> createLayerServer(InputStream in, OutputStream out) throws IOException {
        byte[] secret = DiffieHellmanGenerator.generateKeyServer(in, out);
        out = new ObfuscationOutputStream(out, secret);
        out.flush();
        in = new ObfuscationInputStream(in, secret);
        return Tuple.of(in, out);
    }
    
}
